<template>
  <div class="qualiy_add">
    <el-dialog title="导出周报信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="form" class="demo-form-inline" :model="form" label-width="120px">
        <el-form-item label="导出范围">
          <el-date-picker
            v-model="datevalue"
            type="datetimerange"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="进口">
          <el-input type="textarea" v-model="form.putin"></el-input>
        </el-form-item>
        <el-form-item label="出口">
          <el-input type="textarea" v-model="form.out"></el-input>
        </el-form-item>
        <el-form-item label="斜井">
          <el-input type="textarea" v-model="form.inclined"></el-input>
        </el-form-item>
        <el-form-item label="结论">
          <el-input type="textarea" v-model="form.conclusion"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="create">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
  <script>
import api from '../../../api/base.js'
import qs from 'qs'
import moment from 'moment'
import jsCookie from 'js-cookie'

import fileDownload from 'js-file-download'
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      dialogVisible: false,
      form: {
        tId: '', //隧道
        putin: '', //进口口
        out: '', // 出口
        inclined: '', //斜井
        conclusion: '', // 结论
        startDateTime: '', // 开始时间
        endDateTime: '' // 结束时间
      },
      datevalue: [new Date(), new Date()]
    }
  },
  mounted() {
    this.$bus.$on('MonitorInfoWeekShow', (flag) => {
      if (this.form.tId == ''){
        this.$message({
          type: 'fail',
          message: '请选择隧道'
        })
        this.dialogVisible = false
      }else{
        this.dialogVisible = true
      }
    })
    this.$bus.$on('MonitorReportWeekFid', (flag) => {
      this.form.tId = flag
    })
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    create() {

      this.form.startDateTime = moment(this.datevalue[0]).format('YYYY-MM-DD') + ' 00:00:00'
      this.form.endDateTime = moment(this.datevalue[1]).format('YYYY-MM-DD') + ' 23:59:59'
      const token = jsCookie.get("token")
      const url =  api.baseUrl + api.monitorMeasurementGenerateweekreport+'?' + qs.stringify(this.form)
      var xhr = new XMLHttpRequest();
      xhr.open("get", url, true); // get、post都可
      xhr.responseType = "blob";  // 转换流
      xhr.setRequestHeader("Authorization", token); // token键值对
      xhr.onload = function() {
          if (this.status == 200) {
              var blob = this.response;
              var a = document.createElement("a")
              var url = window.URL.createObjectURL(blob)
              a.href = url
              a.download = "周报.docx"  // 文件名
          }
       a.click()
       window.URL.revokeObjectURL(url)
       }
       xhr.send();

      // api
      //   .monitorMeasurementGenerateweekreport(this.form)
      //   .then((res) => {
      //     fileDownload(res.data, 'deviceModel.docx');
      //   })
      //   .catch((err) => {
      //     console.log(err)
      //   })
    }
  }
}
</script>
  
  <style scoped>
.qualiy_add {
  /* width: 1000px;
  margin: 10px; */
}
.el-dialog {
  width: 40%;
}
</style>